//package com.cyzy.utils;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.springframework.stereotype.Component;
//import org.springframework.web.socket.*;
//
//
//import java.util.HashSet;
//import java.util.Set;
//import java.util.concurrent.ConcurrentHashMap;
//
//@Component
//public class ChatWebSocketHandler implements WebSocketHandler {
//
//
//
//    private static Set<WebSocketSession> sessions = new HashSet<>();
//
//
//    private static ConcurrentHashMap<String, WebSocketSession> webSocketMap = new ConcurrentHashMap<>();
//
//
//
//    @Override
//    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
//        sessions.add(session);
//        System.out.println("看看里面到底有什么" + session);
//    }
//
//    @Override
//    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
//        ObjectMapper objectMapper = new ObjectMapper();
//
//
//
//        // 转发消息给目标用户
//        for (WebSocketSession webSocketSession : sessions) {
//            if (webSocketSession.isOpen()) {
//                System.out.println("我连接了吗？？？？？？？？？？？？？？？？");
//            }
//        }
//    }
//
//    @Override
//    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
//        sessions.remove(session);
//    }
//
//    @Override
//    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
//        sessions.remove(session);
//    }
//
//    @Override
//    public boolean supportsPartialMessages() {
//        return false;
//    }
//
//}
//////=====================================================================================================
////@Component
////public class ChatWebSocketHandler implements WebSocketHandler {
////
////
////    private static Set<WebSocketSession> sessions = new HashSet<>();
////
////
////    private static ConcurrentHashMap<String, WebSocketSession> webSocketMap = new ConcurrentHashMap<>();
////
////
////
////    @Override
////    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
////        sessions.add(session);
////        System.out.println("看看里面到底有什么" + session);
////        // 从 session 的 URI 中提取 userId
////        String uri = session.getUri().toString();
////        String userId = uri.substring(uri.lastIndexOf("/") + 1); // 提取最后一个斜杠后的部分
////
////        if (userId == null || userId.isEmpty()) {
////            System.err.println("User ID is null or empty! Cannot establish connection.");
////            return; // 或者抛出异常
////        }
////
////        webSocketMap.put(userId, session); // 确保将会话添加到 map 中
////        System.out.println(userId + " - 连接建立成功...");
////        printOnlineUsers();
////    }
////    @Override                        //用户1    用户2           ID 1     ID2
////    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
////        ObjectMapper objectMapper = new ObjectMapper();
////
//////        使用 ObjectMapper 将消息的负载（payload）转换为 Message 对象。
////        JsonNode jsonNode = objectMapper.readTree(message.getPayload().toString());
////
////        String event = jsonNode.get("event").asText();
////        String userid = jsonNode.get("userid").asText();
////        JsonNode data = jsonNode.get("data");
////
////
////
////
////        switch (event) {
////            case "offer":
////            case "answer":
////            case "_ice_candidate":
////                // 处理 WebRTC 相关的事件
////                sendMessageToUser(message.getPayload().toString());
////                break;
////            default:
////                System.out.println("未知事件类型: " + event);
////        }
////
////    }
////
////    private void sendMessageToUser(String message) throws IOException {
////        for (WebSocketSession session : webSocketMap.values()) {
////            if (session.isOpen()) {
////                session.sendMessage(new TextMessage(message));
////                System.out.println("发送给所有人！！！");
////            } else {
////                System.out.println("某个用户的连接已关闭，无法发送消息");
////            }
////        }
////    }
////
////
////    @Override
////    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
////        sessions.remove(session);
////    }
////
////    @Override
////    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
////        sessions.remove(session);
////        String userId = session.getUri().toString().substring(session.getUri().toString().lastIndexOf("/") + 1);
////        webSocketMap.remove(userId);
////        System.out.println(userId + " - 连接已关闭...");
////        printOnlineUsers();
////    }
////
////    @Override
////    public boolean supportsPartialMessages() {
////        return false;
////    }
////
////    private void printOnlineUsers() {
////        String onlineUsers = webSocketMap.keySet().stream().collect(Collectors.joining(", "));
////        System.out.println("当前在线用户: " + onlineUsers);
////    }
////}
